
#include "ui_setting.h"
#include "lcd.h"
#include <string.h>
#include "esp_log.h"
#include "custom_nvs.h"
#include "app_sr.h"
#include "screenshot.h"

static ui_setting_t g_ui_setting;

/*从nvs中读取设置*/
esp_err_t load_ui_setting(void)
{
    /*从NVS中读取dap设置*/
    esp_err_t ret =ESP_OK;
    ret = custom_nvs_read(UI_SETTING, &g_ui_setting, sizeof(ui_setting_t));
    if (g_ui_setting.magic != UI_SETTING_MAGIC)
    {
        /*数据要么丢失，要么版本不匹配，重新设置默认值*/
        ui_setting_load_default(true);
    }
    ui_setting_load_after_boot();
    return ret;
}
esp_err_t set_ui_setting(ui_setting_t* pUi_Setting, bool is_to_flash)
{
    if (pUi_Setting == NULL) {
        goto load;
    }
    memcpy(&g_ui_setting, pUi_Setting, sizeof(ui_setting_t));

load:
    //保存设置到NVS
    esp_err_t ret =ESP_OK;

    if (is_to_flash) {
        ret = custom_nvs_write(UI_SETTING, &g_ui_setting, sizeof(ui_setting_t));
    }

    return ret;
}
esp_err_t ui_setting_load_default(bool is_to_flash)
{
    g_ui_setting = UI_SETTING_DEFAULT();
    ui_setting_load_after_boot();
    return set_ui_setting(NULL, is_to_flash);
}

/***********************语言设置****************************/
esp_err_t set_screen_language(lang_type_t language, bool is_to_flash)
{
    set_language(language);
    g_ui_setting.language = get_language();
    return set_ui_setting(NULL, is_to_flash);
}

lang_type_t get_screen_language(void)
{
    return g_ui_setting.language;
}
/***********************语言设置 END****************************/
/***********************屏幕颜色设置****************************/
static uint32_t screen_color = 0;
esp_err_t set_screen_color(screen_color_t color, bool is_to_flash)
{
    
    switch (color) {
        case SCREEN_COLOR_WHITE://白色
            screen_color = 0xFFFFFF;
            break;
        case SCREEN_COLOR_BLACK://黑色
            screen_color = 0x000000;
            break;
        case SCREEN_COLOR_CYAN: //青色
            screen_color = 0x00FFFF;
            break;
        case SCREEN_COLOR_GRAY: //灰色
            screen_color = 0x808080;
            break;
        default:
            screen_color = 0xFFFFFF;
            break;
    }
     g_ui_setting.screen_color = color;
     return set_ui_setting(NULL, is_to_flash);
}
uint32_t get_screen_color_val(void)
{
    return screen_color;    
}
screen_color_t get_screen_color(void)
{
    return g_ui_setting.screen_color;
}

/**
 * @brief 获取按钮字体颜色
 * @return 根据屏幕主题返回适配的字体颜色
 */
lv_color_t get_btn_font_color(void)
{
    screen_color_t color = get_screen_color();
    /* 深色模式返回白色文字，浅色模式返回深灰色文字 */
    if (color == SCREEN_COLOR_BLACK) {
        return COLOR_WHITE; // 深色模式用白色文字
    } else {
        return COLOR_BLACK; // 浅色模式用深灰色文字
    }
}
/***********************屏幕颜色设置 END****************************/
/***********************屏幕亮度设置****************************/
/*
 * @brief 设置屏幕亮度
 * @param bright 亮度值，范围10-100
*/
esp_err_t set_screen_bright(uint8_t bright, bool is_to_flash)
{
    /*0就黑屏了,限制最小亮度为10*/
    if (bright<10)
        bright = 10;
    bsp_display_brightness_set(bright);
    g_ui_setting.screen_bright = bright;
    return set_ui_setting(NULL, is_to_flash);
}
uint8_t get_screen_bright(void)
{
    return g_ui_setting.screen_bright;
}
/***********************屏幕亮度设置 END****************************/
/***********************语言设置****************************/
esp_err_t set_voice_control(bool is_open, bool is_to_flash)
{
    g_ui_setting.voice_control = is_open;
    if (is_open){
        app_sr_init();//开启语音识别
    }else{
        app_sr_deinit();//
    }
    return set_ui_setting(NULL, is_to_flash);
}
bool get_voice_control(void)
{
    return g_ui_setting.voice_control;
}

/***********************语言设置 END****************************/
/***********************截图设置****************************/
esp_err_t set_screenshot_control(bool is_open, bool is_to_flash)
{
    g_ui_setting.screenshot_ctl = is_open;
    if (is_open) {
        screenshot_button_init(); //开启截图功能
    } else {
        screenshot_button_deinit(); //
    }
    return set_ui_setting(NULL, is_to_flash);
}
bool get_screenshot_control(void)
{
    return g_ui_setting.screenshot_ctl;
}

/***********************截图设置 END****************************/
/***************************开机时加载设置后要调用的函数*********************************************/

void ui_setting_load_after_boot(void)
{
    set_screen_bright(get_screen_bright(), false); // 设置屏幕亮度
    set_screen_color(get_screen_color(), false); // 设置屏幕颜色
    set_screen_language(get_screen_language(), false); // 设置语言
    set_screenshot_control(get_screenshot_control(), false); //设置截图
}

void is_open_voice_control(void)
{
    if (get_voice_control()) {
        app_sr_init(); //开启语音识别
    }
}

/*************************************************************************************************/
